## Conference matrix of a finite field

from PyM import *

def paley_matrix(F):
    if characteristic(F)==2:
        return "paley_matrix error: the characteristic of F must not be 2"
    q = cardinal(F)
    def x(j): return element(j,F)
    def chi(a): return legendre(a,F)
    return matrix([[chi(x(i)-x(j)) for j in range(q)] for i in range(q)])

def conference_matrix(K):
    q = cardinal(K)
    e = ((q-1)//2)%2
    e = (-1)**e
    S = paley_matrix(K)
    u = matrix(q*[1])
    x = splice(matrix([0]),u)
    X = splice(transpose(e*u),S)
    return stack(x,X)
    
show(conference_matrix(Zn(3)))